import { AxiosError, AxiosResponse } from 'axios';
import { WithError } from './interfaces';

export async function responseWithError<T>(
  response: Promise<T>,
): Promise<WithError<T>> {
  let data: T | null = null;
  let err: AxiosResponse<T> | null = null;

  try {
    data = await response;
  } catch (error) {
    if (error instanceof AxiosError && error.response) {
      err = error.response;
    }
  }

  return { data, err };
}
